<?php
    $aut = new bgAuthorize();
 ?>
<form action="index.php?option=com_questionare" method="post" name="adminForm" id="adminForm" enctype="multipart/form-data">
	<fieldset id="filter-bar">
		<div class="filter-search fltlft">
			<label class="filter-search-lbl" for="filter_search">フィルタ</label>
			<input type="text" name="filter_search" id="filter_search" value="<?php echo JRequest::getVar('filter_search') ?>" title="Search title or alias. Prefix with ID: to search for an article ID.">
			<button type="submit" class="btn">検索</button>
			<button type="button" onclick="document.id('filter_search').value='';this.form.submit();">クリア</button>
		</div>
        <div style="float: right;">
            <button type="submit" class="btn" href="index.php?option=com_question">質問管理</button>
        </div>
		<div style="float: right;">
            <button type="submit" class="btn" href="index.php?option=com_questionpage">ページ管理</button>
        </div>
        
	</fieldset>
	<div class="clr"> </div>

	<table class="adminlist">
		<thead>
			<tr>
				<th width="1%">
					<input type="checkbox" name="checkall-toggle" value="" title="Check All" onclick="Joomla.checkAll(this)"/>
				</th>
				<th width="10%">
					<label class="labelth">ID</label>				
                </th>
				<th width="15%">
					<label class="labelth">アンケート名</label>		
                </th>
				<th width="15%">
					<label class="labelth">回答人数</label>		
                </th>
				<!-- <th width="9%">
					<a href="#">回答</a>			
                </th> -->
				<th width="15%">
					<label class="labelth">アンケート集計</label>
                </th>
				<th width="10%">
					<label class="labelth">開始</label>
                </th>
                <th width="10%">
					<label class="labelth">完了</label>
                </th>
                <th width="13%">
					<label class="labelth">フロントページ</label>
                </th>
                <th width="13%">
                    <label class="labelth">登録済み/一時保存中</label>
                </th>
			</tr>
		</thead>
		<tfoot>
			<tr>
				<td colspan="15">
				    <?php echo $this->pagination->getListFooter(); ?>	
                </td>
			</tr>
		</tfoot>
		<tbody>
            <?php foreach($this->rows as $k=>$row):?>
                <tr class="row<?php echo $k%2?>">
                    <td class="center">
                        <input type="checkbox" class="chbbox" id="cb<?php echo $row->SERQ_CODE ?>" name="cid[]" value="<?php echo $row->SERQ_CODE ?>" onclick="Joomla.isChecked(this.checked);" />
                    </td>
                    <td class="center">
                        <?php echo $row->SERQ_CODE;?>
                    </td>
                    <td class="center">
                    <?php if($row->SERQ_PUBLISHED =='0' || ($row->SERQ_PUBLISHED =='1' && ( strtotime("now") <= strtotime($row->SERQ_START)))){ ?>
                        <a href="#" onclick="document.id('cb<?php echo $row->SERQ_CODE; ?>').checked=true;Joomla.submitbutton('edit');"> <?php echo $row->SERQ_TITLE;?> </a>
                    <?php }else{ ?>
                        <?php echo $row->SERQ_TITLE;?>
                    <?php } ?>
                    </td>
                    <td class="center">
                        <?php
                            $model = $this->getModel(); 
                            echo $model->countAnsQuestionare($row->SERQ_CODE);
                        ?>
                    </td>
                    <!-- <td class="center">
                    <?php
                        if($aut->getUserGroup() == '8' || $aut->getUserGroup() == '9'){
                        
                    ?>
                        <a id="export" href="#">回答</a>
                        <a href="#" onclick="document.id('cb<?php echo $row->SERQ_CODE; ?>').checked=true;Joomla.submitbutton('csv');" >回答</a>
                    <?php }else{
                        
                    ?>
                    回答
                    <?php
                        }
                    ?> 
                    </td>-->
                    <td class="center">
                        <a href='index.php?option=com_questionare&task=summary&cid=<?php echo $row->SERQ_CODE; ?>' > アンケート集計</a>
                    </td>
                    <td class="center">
                        <?php if($row->SERQ_START != "0000-00-00 00:00:00" || $row->SERQ_START != "") echo date('Y-m-d',strtotime($row->SERQ_START));?>
                    </td>
                    <td class="center">
                        <?php if($row->SERQ_FINISH != "0000-00-00 00:00:00" || $row->SERQ_FINISH != "") echo date('Y-m-d',strtotime($row->SERQ_FINISH));?>
                    </td>
                    <td class="center">
                        <?php
                            if($row->SERQ_PUBLISHED =='1'){ echo '公開';
                        
                            }else if ($row->SERQ_PUBLISHED =='0'){ echo '非公開';
                          } ?>
                        <input type="hidden" name="puplish" id="publish<?php echo $row->SERQ_CODE ?>" value="<?php echo $row->SERQ_PUBLISHED; ?>" />
                        <?php if(strtotime("now") <= strtotime($row->SERQ_START)){ $temp = 0;} else { $temp = 1;} ?>
                        <input type="hidden" name="date" id="date<?php echo $row->SERQ_CODE ?>" value="<?php echo $temp; ?>" />
                    </td>
                    <td class='center'>
                        <?php 
                            ($row->SERQ_FLAG) == 1 ? $savetemp = '登録済み' : $savetemp = '一時保存中'; //Check flag save temp or save
                            echo $savetemp;
                        ?>
                    </td>
             <?php endforeach ?>
		</tbody>
	</table>

	<div>
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="boxchecked" value="0" />
		
</form>
<script>
Joomla.submitbutton = function(task){
	   if(task == 'remove'){
	       if(!confirm('選択したデータを削除します。よろしいですか？')){
	           return false;
	       }
	   }
       var check = "0";
       if(task == 'edit'){
            jq(".chbbox").each(function(){
            if(jq(this).attr('checked')){
                var id = jq(this).val();
                var publish = jq("#publish"+id).val();
                var date = jq("#date"+id).val();
                if(publish == "1" && date == "1"){
                    alert("公開されたアンケートについて、編集不可。")
                    check = "1";
                    return false;
                }
            }
            });
       }
       if(check == "0"){
            Joomla.submitform(task, document.getElementById('adminForm'));
       }else{
            Joomla.submitform("search", document.getElementById('adminForm'));
       }
 }
</script>